package demo.proxy;

import demo.proxy.dynamic.cglib.PayMethodInterceptor;
import demo.proxy.dynamic.jdk.PayInvocationHandler;
import demo.proxy.target.ToCPay;
import demo.proxy.target.impl.ToCPayImpl;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class DynamicProxyTest {
    public static void main(String[] args) {
        // JDK动态代理：
        // ToCPay target = new ToCPayImpl();
        // InvocationHandler aspect = new PayInvocationHandler(target);
        // ToCPay proxy = (ToCPay) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),aspect);
        // proxy.pay();

        // CGLib:
        ToCPay toCPay = (ToCPay) Enhancer.create(ToCPayImpl.class,new PayMethodInterceptor());
        toCPay.pay();
    }
}
